#!/usr/bin/env python
#coding: ISO-8859-1 -*-'

__author__ = 'Ralf Schoenian'

import os
import subprocess
import sys
from unittest import TestCase, main
import SQLhelper

CREATE_FILE = 'create.db.sql'

testpath = os.path.dirname(os.path.abspath(__file__))
workpath = testpath.replace('/test','')
sys.path.append(workpath)

import model

def prepare_test_db():
        '''Prepare a test.db.

        The test.db contains one voc with the id=1.
        '''
        #print model.config.get('Settings','dbfile')

        sqlFile = 'sql/create.db.sql'
        cmd = SQLhelper.create_sql(sqlFile)
        os.system('/bin/rm %s' % (model.config.get('Settings','dbfile')))
        #    print cmd
        os.system(cmd)


class TestModel(TestCase):

    modelObj = model.Model()

    def test_delete_voc(self):
        '''Test the model.delete method.

        How it works:
        First we lookup the voc with id=1. We expect to find 'Kapital'.
        After deleting the same select should return an empty string.
        '''

        sqlFile = 'sql/select.first.voc.sql'
        result = SQLhelper.run_sql(sqlFile)
        self.assertEqual(result, 'Kapital')

        self.modelObj.delete_voc_by_id(1)

        result = SQLhelper.run_sql(sqlFile)
        self.assertEqual(result, '')

    def test_get_oldest_voc(self):
        '''Test the model.get_oldest_voc method.
        '''

        expectedResult = 'Finanzanlagen'
        voc = self.modelObj.get_oldest_voc(category = 'All')
        self.assertEqual(expectedResult, voc.lang1)

    def test_get_oldest_voc_by_category(self):
        '''Test the model.get_oldest_voc method.
        '''

        expectedResult = 'Zielsetzung'
        category = 'misc'
        modelObj = model.Model()
        voc = self.modelObj.get_oldest_voc(category = category)
        self.assertEqual(expectedResult, voc.lang1)

    def test_get_default_category(self):
        '''Result should be "new".   '''

        expectedResult = 'new'
        modelObj = model.Model()
        category = self.modelObj.config.get('Settings','defaultcategory')
        self.assertEqual(expectedResult, category)

    def test_count_all_vocs(self):
        expectedResult = 4
        counted = self.modelObj.count_voc()
        self.assertEqual(expectedResult, counted)

    def test_count_voc_by_category(self):
        '''Expected 3 vocs in category business.'''
        category = 'business'
        expectedResult = 3

        count = self.modelObj.count_voc_by_category(category)
        self.assertEqual(expectedResult, count)

    def test_save_new_voc(self):
        '''Values for both directions should be 0. '''

        lang1 = 'Schlagloch'
        lang2 = 'pothole'
        categories = ['car',]
        errorUpdate = False

        expectedResult = 0

        modelObj = model.Model()
        modelObj.save_new_voc(lang1, lang2, categories, errorUpdate)

        voc = modelObj.get_voc_by_id(5)
        self.assertEqual(voc.err_lang1, expectedResult)
        self.assertEqual(voc.err_lang2, expectedResult)


if __name__ == '__main__':
    prepare_test_db()
    main()
